package cn.seqdata.cxf;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.spring.SpringResourceFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/** setDefaultResourceProvider，主要用于解决setServiceClass的@Scope问题 */
public class SpringJAXRSServerFactoryBean extends JAXRSServerFactoryBean implements ApplicationContextAware {
	private ApplicationContext ctx;

	@Override
	protected void setDefaultResourceProvider(ClassResourceInfo cri) {
		String beanNames[] = ctx.getBeanNamesForType(cri.getServiceClass());
		SpringResourceFactory rp = new SpringResourceFactory(beanNames[0]);
		rp.setApplicationContext(ctx);
		cri.setResourceProvider(rp);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.ctx = applicationContext;
	}
}
